/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.workflow.service.impl;

import me.zhengjie.modules.workflow.domain.ActReDeployment;
import me.zhengjie.modules.workflow.service.dto.ActReDeploymentDto;
import me.zhengjie.modules.workflow.service.dto.ActReDeploymentQueryCriteria;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.workflow.repository.ActReDeploymentRepository;
import me.zhengjie.modules.workflow.service.ActReDeploymentService;
import me.zhengjie.modules.workflow.service.mapstruct.ActReDeploymentMapper;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wzp
* @date 2021-04-18
**/
@Service
@RequiredArgsConstructor
public class ActReDeploymentServiceImpl implements ActReDeploymentService {

    private final ActReDeploymentRepository actReDeploymentRepository;
    private final ActReDeploymentMapper actReDeploymentMapper;

    @Autowired
    private RepositoryService repositoryService;

    @Override
    public Map<String,Object> queryAll(ActReDeploymentQueryCriteria criteria, Pageable pageable){
        Page<ActReDeployment> page = actReDeploymentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(actReDeploymentMapper::toDto));
    }

    @Override
    public List<ActReDeploymentDto> queryAll(ActReDeploymentQueryCriteria criteria){
        return actReDeploymentMapper.toDto(actReDeploymentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ActReDeploymentDto findById(String id) {
        ActReDeployment actReDeployment = actReDeploymentRepository.findById(id).orElseGet(ActReDeployment::new);
        ValidationUtil.isNull(actReDeployment.getId(),"ActReDeployment","id",id);
        return actReDeploymentMapper.toDto(actReDeployment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActReDeploymentDto create(ActReDeployment resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return actReDeploymentMapper.toDto(actReDeploymentRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ActReDeployment resources) {
        ActReDeployment actReDeployment = actReDeploymentRepository.findById(resources.getId()).orElseGet(ActReDeployment::new);
        ValidationUtil.isNull( actReDeployment.getId(),"ActReDeployment","id",resources.getId());
        actReDeployment.copy(resources);
        actReDeploymentRepository.save(actReDeployment);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            //依据activiti提供API进行删除
            Deployment deployment = repositoryService
                    .createDeploymentQuery()
                    .deploymentId(id)
                    .singleResult();
            if(deployment != null){
                repositoryService.deleteDeployment(deployment.getId());
            }
        }
    }

    @Override
    public void download(List<ActReDeploymentDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ActReDeploymentDto actReDeployment : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" name",  actReDeployment.getName());
            map.put(" category",  actReDeployment.getCategory());
            map.put(" key",  actReDeployment.getKey());
            map.put(" tenantId",  actReDeployment.getTenantId());
            map.put(" deployTime",  actReDeployment.getDeployTime());
            map.put(" engineVersion",  actReDeployment.getEngineVersion());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}